package com.smartstate.EnvironmentMonitor.config;



import com.smartstate.EnvironmentMonitor.util.SerialUtil;
import com.sun.xml.txw2.output.ResultFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

/**
 * 通用configure
 */
@Configuration
public class Config {
  @Value("${spring.messages.basename}")
  private String basename;

  @Value("${spring.messages.cache-seconds}")
  private long cacheMillis;

  @Value("${spring.messages.encoding}")
  private String encoding;

  /**
   * redis处理对象
   */
  @Autowired
  StringRedisTemplate stringRedisTemplate;

  /**
   * 作用：自动包装成ResultVO，如果想要自己处理。需要添加注解@ApiResultIgnore
   * @return 全局统一结果处理类
   */
//  @Bean
//  ApiResultHandler getApiResultHandler() {
//    return new ApiResultHandler();
//  }

  /**
   * 作用：自动生成序列号,默认格式为YYYYMMDD0000,如202203010001
   * @return 序列号
   */
  @Bean
  SerialUtil getSerialUtil() {
    return new SerialUtil(stringRedisTemplate);
  }

  /**
   * 作用：websocket
   */
  @Bean
  public ServerEndpointExporter serverEndpointExporter() {
    ServerEndpointExporter serverEndpointExporter = new ServerEndpointExporter();
    return serverEndpointExporter;
  }

  @Bean(name="redisTemplate")
  public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
    RedisTemplate<String, String> template = new RedisTemplate<>();
    RedisSerializer<String> redisSerializer = new StringRedisSerializer();
    template.setConnectionFactory(factory);
    //key序列化方式
    template.setKeySerializer(redisSerializer);
    //value序列化
    template.setValueSerializer(redisSerializer);
    //value hashmap序列化
    template.setHashValueSerializer(redisSerializer);
    //key haspmap序列化
    template.setHashKeySerializer(redisSerializer);
    //
    return template;
  }


}
